package com.ram.testcoherence.test;

import com.ram.testcoherence.ejb.CoherenceSessionEJB;

import com.ram.testcoherence.entity.Person;

import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class CoherenceSessionEJBClient {
    public static void main(String[] args) {
        try {
            final Context context = getInitialContext();
            CoherenceSessionEJB coherenceSessionEJB =
                (CoherenceSessionEJB)context.lookup("CoherenceSessionEJB#com.ram.testcoherence.ejb.CoherenceSessionEJB");
                        Person personx = new Person();
                        personx.setName("Anush");
                        personx.setPersonid(2L);
                        personx.setPhone(5127087788L);
                        coherenceSessionEJB.mergePerson(personx);
//            List<Person> personList = coherenceSessionEJB.getPersonFindAll();
//            for (Person person : personList) {
//               person.setName(person.getName().substring(0, 8));
//                coherenceSessionEJB.mergePerson(person);
//                for(int j=0; j<100; j++){
//                    person.setPersonid(person.getPersonid()+1+j);
//                    coherenceSessionEJB.mergePerson(person);
//                }
//                printPerson(person);
//            }
//            
//            
//                        Person person = coherenceSessionEJB.getPersonFromCache(2222L);
//                        Person person1 = coherenceSessionEJB.getPersonFromCache(1115L);
//                        Person person2 = coherenceSessionEJB.getPersonFromCache(1116L);
//                        Person person3 = coherenceSessionEJB.getPersonFromCache(1117L);
//            
//                        System.out.println("Success" + person != null ?
//                                           "person not exist" : person.getName());
//                        System.out.println("Success" + person1.getName());
//                        System.out.println("Success" + person2.getName());
//                        System.out.println("Success" + person3.getName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void printPerson(Person person) {
        System.out.println("name = " + person.getName());
        System.out.println("personid = " + person.getPersonid());
        System.out.println("phone = " + person.getPhone());
    }

    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x connection details
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL,
                "t3://192.168.2.13:8003,192.168.2.13:8004");
        return new InitialContext(env);
    }
}
